package com.example.player.controllerView;

import android.content.Context;
import android.os.Message;
import android.view.View;
import android.widget.SeekBar;

import com.easefun.polyvsdk.video.PolyvBaseMediaController;
import com.easefun.polyvsdk.video.PolyvVideoView;
import com.example.player.PolyvSPUtils;
import com.example.player.PolyvTimeUtils;

import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class PlayerMediaController extends PolyvBaseMediaController implements View.OnClickListener {
    private SeekBar sb_player;
    private static final int longTime = 5000;
    private static final int HIDE = 12;
    private static final int SHOW_PROGRESS = 13;
    private static final int SAVE_PROGRESS = 30;
    private PolyvVideoView videoView=null;

    //进度条拖拽跳转播放进度策略
    public static final int DRAG_SEEK_ALLOW = 0;//允许拖动进度条跳转进度
    public static final int DRAG_SEEK_BAN = 1;//禁止拖动进度条跳转进度
    public static final int DRAG_SEEK_PLAYED = 2;//只允许在已播放进度区域拖动跳转播放进度
    private int dragSeekStrategy = DRAG_SEEK_PLAYED;
    private OnDragSeekListener onDragSeekListener;

    private Handler handler=new Handler() {
        @Override
        public void publish(LogRecord record) { }
        @Override
        public void flush() { }
        @Override
        public void close() throws SecurityException { }

        public void handleMessage(Message msg){
            switch (msg.what){
                case HIDE:
                    hide();
                    break;
//                case SHOW_PROGRESS:
//                    showProgress();
//                    break;
//                case SAVE_PROGRESS:
//                    saveProgress();
//                    break;
            }
        }
    };

    public PlayerMediaController(Context context) {
        super(context);
    }

    private int getPosition(){
        if (videoView != null) {
            // 单位：毫秒
            int position = videoView.getCurrentPosition();
            int totalTime = videoView.getDuration() / 1000 * 1000;
            if (!videoView.isExceptionCompleted() && (videoView.isCompletedState() || position > totalTime))
                position = totalTime;
            return position;
        }
        return 0;
    }
    private int getSavePosition() {
        if (videoView != null && videoView.getCurrentVid() != null) {
            //保存当前播放进度
            return PolyvSPUtils.getInstance(getContext(), "videoProgress").getInt(videoView.getCurrentVid());
        }
        return 0;
    }
    @Override
    public boolean canDragSeek(int seekPosition) {
        boolean canDragSeek = true;
        if (dragSeekStrategy == DRAG_SEEK_PLAYED) {
            canDragSeek = seekPosition <= getSavePosition();
        } else if (dragSeekStrategy == DRAG_SEEK_BAN) {
            canDragSeek = false;
        }

        if (onDragSeekListener == null) {
            return canDragSeek;
        }
        if (canDragSeek) {
            onDragSeekListener.onDragSeekSuccess(videoView.getCurrentPosition(), seekPosition);
        } else {
            onDragSeekListener.onDragSeekBan(dragSeekStrategy);
        }
        return canDragSeek;
    }
    private void saveProgress() {
        if (videoView != null && videoView.getCurrentVid() != null) {
            // 单位：毫秒
            int position = getPosition();
            //保存当前播放进度
            int maxPosition = PolyvSPUtils.getInstance(getContext(), "videoProgress").getInt(videoView.getCurrentVid());
            if (position > maxPosition) {
                PolyvSPUtils.getInstance(getContext(), "videoProgress").put(videoView.getCurrentVid(), position);
            }
            //handler.sendEmptyMessageDelayed(SAVE_PROGRESS, 3000);
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void hide() {

    }

    @Override
    public boolean isShowing() {
        return false;
    }

    @Override
    public void setAnchorView(View view) {

    }

    @Override
    public void show(int i) {

    }

    @Override
    public void show() {

    }


    public interface OnDragSeekListener {
        void onDragSeekSuccess(int positionBeforeSeek, int positionAfterSeek);

        void onDragSeekBan(int dragSeekStrategy);
    }
}
